﻿#include "spaceblock.h"
#include "ui_spaceblock.h"

SpaceBlock::SpaceBlock(const MessageStatus &status,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::SpaceBlock)
{
    ui->setupUi(this);
    switch (status) {
    case MessageStatus::SUCCESS:{}break;
    case MessageStatus::LOADING:{
        this->loading=new QLabel();
        this->loading->setFixedSize(35,35);
        sending();
    }break;
    case MessageStatus::FAILURE:{
        this->loading=new QLabel();
        this->loading->setFixedSize(35,35);
        sendFail();
    }
    }
}
void SpaceBlock::sendFinish(){
    QMovie* movie=loading->movie();
    if(movie!=nullptr){
        delete loading;
        loading=nullptr;
//        loading->clear();
    }
}
void SpaceBlock::sending(){
    QMovie *movie=new QMovie(":/background/imagesRes/loading.gif");
    movie->setScaledSize(QSize(25,25));
    loading->setMovie(movie);
    movie->start();
}
void SpaceBlock::sendFail(){
    QMovie *movie=loading->movie();
    if(movie!=nullptr)
        loading->movie()->deleteLater();
    loading->setPixmap(QPixmap(":/background/imagesRes/error.png"));
}
void SpaceBlock::insertLeft(){
    if(loading!=nullptr)
        ui->horizontalLayout->insertWidget(0,loading);
}

void SpaceBlock::insertRight(){
    if(loading!=nullptr)
    ui->horizontalLayout->addWidget(loading);
}

SpaceBlock::~SpaceBlock()
{
    if(loading!=nullptr){
        delete loading;
        loading=nullptr;
    }
    delete ui;
}
